/*************************************************************************
 * File Name:    Longest_Common_Prefix.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/9 11:00:31
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Longest Common Prefix
 | Write a function to find the longest common prefix string amongst an array of strings.  
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    string longestCommonPrefix(vector<string> &strs)
    {
        if (strs.size() == 0) return "";
        if (strs.size() == 1) return strs[0];
        
        string ans;
        bool same;
        
        for (size_t i = 0; i < strs[0].size(); ++i) {
            same = true;
            for (size_t j = 1; j < strs.size(); ++j) {
                if (strs[j].size() <= i) return ans;
                if (strs[j][i] != strs[0][i]) same = false;
            }
            if (!same) break;
            ans += strs[0][i];
        }
        return ans;
    }
};

